<template>
  <div class="app-container">
    <el-table
      v-loading="isLoading"
      element-loading-text="拼命加载中"
      element-loading-spinner="el-icon-loading"
      element-loading-background="rgba(0, 0, 0, 0.8)"
      :data="list"
      stripe
      border
      style="width: 100%"
    >
      <el-table-column type="index" label="序号" width="50" />
      <el-table-column label="主图">
        <template slot-scope="scope">
          <img style="width:70px; display: block;margin: 0 auto;"
            :src="scope.row.coverImg | resetImg">
        </template>
      </el-table-column>
      <el-table-column prop="name" label="名字" width="180" />
      <el-table-column prop="price" label="价格" width="180" />
      <el-table-column prop="quantity" label="库存" />
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      layout="prev, pager, next"
      @current-change="pageHandle"
      background
      :total="totals"
    />
  </div>
</template>

<script>
// @ is an alias to /src
import { mapState, mapActions } from 'vuex'
import { delOne } from '../../api/products'

export default {
  name: 'Home',
  data() {
    return {}
  },
  computed: {
    ...mapState('product', ['pages', 'totals', 'list', 'isLoading'])
  },
  created() {
    this.loadData()
  },
  methods: {
    ...mapActions('product', ['loadData']),
    handleEdit(index, item) {
      console.log(index)
      console.log(item)
    },
    handleDelete(index, item) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          await delOne(item._id)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.loadData()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    pageHandle(page) {
      console.log(page)
      this.loadData(page)
    }
  }
}
</script>
